<!--
 * @Author: [lcy]
 * @Date: 2021-10-28 16:13:36
 * @LastEditors: [lcy]
 * @LastEditTime: 2021-10-29 10:01:16
 * @Description: 
-->
<template>
  <!-- 发布资讯页面 -->
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="50px" >
    <div style="float:left">
        <el-form-item label="标题" prop="title" >
        <el-input v-model="form.title" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="分类" prop="categoryId" >
        <el-select v-model="form.categoryId" placeholder="请选择栏目" style="width: 300px;">
          <el-option
            v-for="(item, index) in category"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </div>
      <el-form-item label="封面" style="float:right; margin-right:100px">
        <el-upload
          class="avatar-uploader"
          :action="uploadFileURL"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>

        </el-upload>
      </el-form-item>
      <el-form-item label="正文" prop="content" style=" clear:both;">
        <tinymce v-model="form.content" :height="200" />
      </el-form-item>
      <!-- 一个返回按钮，一个提交按钮 -->
      <el-form-item>
        <div style="text-align: right">
          <el-button type="primary" @click="comeBack">返回</el-button>
          <el-button type="success" @click="submit">发布</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { findAll } from '@/api/zixunfl'
import { showFileURL, uploadFileURL } from '@/utils/config'
import { saveOrUpdate } from '@/api/article'
import Tinymce from '@/components/Tinymce'
export default {
  components: { Tinymce },
  data() {
    return {
      // 先获取所有栏目信息
      category: [],
      // 表单数据
      form: {
        title: '',
        categoryId: '',
        cover: '',
        content: `<h1>123</h1>`
      },
      // 图片url
      imageUrl: '',
      showFileURL,
      uploadFileURL,
      // 表单验证
      rules: {
        title: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        categoryId: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  created() {
    // 先获取所有栏目信息
    this.findCategory()
  },
  mounted() {},
  methods: {
    // 先获取所有栏目信息
    async findCategory() {
      const res = await findAll()
      this.category = [...res.data]
    },
    onSubmit() {
      console.log('submit!')
    },
    // 图片上传成功的回调函数
    handleAvatarSuccess(res, file) {
      // console.log(res, "==图片上传成功之后的res");
      this.imageUrl = URL.createObjectURL(file.raw)
      // console.log(this.imageUrl);
      this.form.cover = showFileURL + res.data.id
    },
    // comeBack
    comeBack() {
      this.$router.go(-1)
    },
    // 提交
    submit() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          const res = await saveOrUpdate(this.form)
          this.form = {}
          this.$router.go(-1)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
